package com.macrosoft.frpd;

/**
 * @author aizaz.haider 
 */
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.macrosoft.frpd.bo.FeedItem;
import com.macrosoft.frpd.bo.ResponseStatus;
import com.macrosoft.frpd.parser.AbstractParser;
import com.macrosoft.frpd.parser.ParserFactory;
import com.macrosoft.frpd.parser.ParserFactory.PARSERS;
import com.macrosoft.frpd.parser.helper.IPublishResultListener;
import com.macrosoft.frpd.parser.helper.PublishResultHelper;
import com.macrosoft.frpd.parser.helper.TorrentFeedBO;
import com.macrosoft.frpd.utils.Logger;

public class MainActivity extends Activity implements IPublishResultListener {
	private AbstractParser parser = null;
	private LinearLayout llConatiner = null;
	private ProgressBar progress = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		parser = ParserFactory.getParserFor(this, PARSERS.FENOPY);
		try {
			llConatiner = (LinearLayout) findViewById(R.id.ll_container);
			progress = (ProgressBar) findViewById(R.id.progress_bar);

			showWaitDialog();
			PublishResultHelper.getInstance().addPublishListener(this);
			parser.parseMainCategory(this.getClass().hashCode(),parser.getXMLFeedMainCategoryURL());

		} catch (Exception e) {
			Logger.log(this.getClass().getCanonicalName(), e);
		}
	}

	// public void publishCatResult(List<FeedItem> pItem) {
	// for (int i = 0; i < pItem.size(); i++) {
	// setTextView(llConatiner, pItem.get(i).getTitle(), pItem.get(i).getURL());
	// if(pItem.get(i).hasChildren()){
	// for(int j = 0; j < pItem.get(i).getChildren().size(); j++){
	// setTextView(llConatiner, pItem.get(i).getChildren().get(j).getTitle(),
	// pItem.get(i).getChildren().get(j).getURL());
	// }
	// }
	// }
	// hideWaitDialog();
	// }

	// public void publishFeedResult(List<TorrentFeedBO> pItem) {
	// llConatiner.removeAllViews();
	// for (int i = 0; i < pItem.size(); i++) {
	// setTextView(llConatiner, pItem.get(i).title,
	// pItem.get(i).downloadURL);
	// }
	//
	// hideWaitDialog();
	// }

	private void setTextView(final LinearLayout llConatiner, String pText,
			String pTag) {
		TextView tv = new TextView(this, null,
				android.R.attr.textAppearanceLarge);

		tv.setText(pText);
		tv.setTag(pTag);

		tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View paramView) {

				try {
					showWaitDialog();
					parser.parseTorrentFeed(this.getClass().hashCode(),paramView.getTag().toString());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		llConatiner.addView(tv);

	}

	private void showWaitDialog() {
		if (progress != null)
			progress.setVisibility(ProgressBar.VISIBLE);
	}

	private void hideWaitDialog() {
		if (progress != null)
			progress.setVisibility(ProgressBar.GONE);
	}

	@Override
	public void publishResultCategoryHTML(int requestCode,ResponseStatus status,List<FeedItem> pItem) {
		for (int i = 0; i < pItem.size(); i++) {
			setTextView(llConatiner, pItem.get(i).getTitle(), pItem.get(i)
					.getURL());
			if (pItem.get(i).hasChildren()) {
				for (int j = 0; j < pItem.get(i).getChildren().size(); j++) {
					setTextView(llConatiner, pItem.get(i).getChildren().get(j)
							.getTitle(), pItem.get(i).getChildren().get(j)
							.getURL());
				}
			}
		}
		hideWaitDialog();
	}

	@Override
	public void publishResultFeedXML(int requestCode,ResponseStatus status,List<TorrentFeedBO> pItem) {
		llConatiner.removeAllViews();
		for (int i = 0; i < pItem.size(); i++) {
			setTextView(llConatiner, pItem.get(i).title,
					pItem.get(i).downloadURL);
		}

		hideWaitDialog();

	}

}
